export function formatDuration(duration: number) {
    let min = Math.floor(duration / 1000 / 60);
    let sec = Math.floor((duration / 1000) % 60);
    return `${min}:${sec < 10 ? "0" + sec : sec}`;
}

export function formatFileSize(size: number) {
    size = size / 1024;
    if (size < 1024) {
        return size.toFixed(2) + "kb";
    }
    size = size / 1024;
    if (size < 1024) {
        return size.toFixed(2) + "Mb";
    }
    size = size / 1024;
    return size.toFixed(2) + "Gb";
}

/**
 * @description: 节流
 * @param {Function} fn
 * @param {number} duration
 * @return {*}
 * @author: tao wenbo 2419421809@qq.com
 */
export function throuttle(fn: Function, duration: number = 100) {
    var timer: any = null;
    return function ( ...args: any[] ) {
        if( timer ){
            return
        }
        timer = setTimeout(function () {
            timer = null;
            return fn.apply(null, args);
        }, duration);
    };
}


/**
 * @description: 将blob保存成文件
 * @param {string} blob
 * @param {string} filename
 * @return {*}
 * @author: tao wenbo 2419421809@qq.com
 */
export function saveFile(blob: string, filename: string){
    if ("download" in document.createElement("a")) {
        // 非IE下载
        const elink = document.createElement("a");
        elink.download = filename;
        elink.style.display = "none";
        elink.href = blob as string;
        document.body.appendChild(elink);
        elink.click();
        window.URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
    } else {
        // IE10+下载
        (navigator as any).msSaveBlob(blob, filename);
    }
}
